package com.gitee.srurl.core.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.core.exception.AssertException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author zxw
 * @date 2023/4/9 17:05
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class AssertUtil {

    public static <T> void commonCheck(T params, Predicate<T> predicate, String errorTemplate, Object... errorParams) {

        if (StrUtil.isBlank(errorTemplate)) {
            errorTemplate = "参数校验不通过！";
        }

        if (!predicate.test(params)) {
            throw new AssertException(StrUtil.format(errorTemplate, errorParams));
        }
    }

    public static void notBlank(String params, String errorTemplate, Object... errorParams) {

        commonCheck(params, StrUtil::isNotBlank, errorTemplate, errorParams);
    }

    public static void isBlank(String params, String errorTemplate, Object... errorParams) {

        commonCheck(params, StrUtil::isBlank, errorTemplate, errorParams);
    }

    public static void nonNull(Object params, String errorTemplate, Object... errorParams) {

        commonCheck(params, Objects::nonNull, errorTemplate, errorParams);
    }

    public static void isNull(Object params, String errorTemplate, Object... errorParams) {

        commonCheck(params, Objects::isNull, errorTemplate, errorParams);
    }

    public static void isEmpty(Collection<?> params, String errorTemplate, Object... errorParams) {

        commonCheck(params, CollectionUtil::isEmpty, errorTemplate, errorParams);
    }

    public static void notEmpty(Collection<?> params, String errorTemplate, Object... errorParams) {

        commonCheck(params, CollectionUtil::isNotEmpty, errorTemplate, errorParams);
    }

    public static void isEmpty(Map<?, ?> params, String errorTemplate, Object... errorParams) {

        commonCheck(params, CollectionUtil::isEmpty, errorTemplate, errorParams);
    }

    public static void notEmpty(Map<?, ?> params, String errorTemplate, Object... errorParams) {

        commonCheck(params, CollectionUtil::isNotEmpty, errorTemplate, errorParams);
    }

    public static void notEmpty(Object[] data, String errorTemplate, Object... errorParams) {

        commonCheck(data, ArrayUtil::isNotEmpty, errorTemplate, errorParams);
    }

    public static void notEmpty(byte[] data, String errorTemplate, Object... errorParams) {

        commonCheck(data, ArrayUtil::isNotEmpty, errorTemplate, errorParams);
    }
}